package br.ufrn.plugin.pewsrt.popup.actions;

import gerado_Csharp.GeradorCSharp;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import tools.IOArquivo;

import analisador_PEWS.AnalisadorPEWSXML;

public class Action implements IObjectActionDelegate {

	private Shell shell;
	private String xpews;
	private String pastaProjetoXpews;
	
	/**
	 * Constructor for Action1.
	 */
	public Action() {
		super();
	}

	/**
	 * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
	 */
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	/**
	 * @see IActionDelegate#run(IAction)
	 */
	public void run(IAction action) {
		File file = new File("Workflow");  
		file.mkdir();
		
		AnalisadorPEWSXML analisadorXML = new AnalisadorPEWSXML();		
		
		if(IOArquivo.initPlugin()){
			analisadorXML.readerXML(pegarCaminhoDoArquivo(this.xpews));
			analisadorXML.gerarProxyWSDL();
			GeradorCSharp csharp = new GeradorCSharp("Music.xml", analisadorXML.getListMetodosServico(), analisadorXML.getListMetodosPartialClass() , analisadorXML.getConstantes(), analisadorXML.getIfElseCondition(), analisadorXML.getWhileCondition());
			csharp.gerarClassMain();
			csharp.wcf(analisadorXML.getArquivosCS());
			
			MessageDialog.openInformation(
					shell,
					"PEWS-RT",
					"O executável gerado foi salvo na pasta:  " + file.getAbsolutePath());
			
			MessageDialog.openInformation(
					shell,
					"PEWS-RT",
					csharp.getLogWfc());
			
			try {
				this.finalize();
			} catch (Throwable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			MessageDialog.openInformation(
					shell,
					"PEWS-RT",
					"Não foi possível encontrar o caminho das ferramentas necessárias para o funcionamento do plugin." +
					"\nPor favor, ir em: Windows > Preferences > PewsRT Preferences, para corrigir");
		}
	}

	/**
	 * @see IActionDelegate#selectionChanged(IAction, ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		xpews = selection.toString();
	}
	
	public String pegarCaminhoDoArquivo(String xpews){
		String arquivo = xpews.substring(3, xpews.length() - 1);
		arquivo = arquivo.substring(arquivo.indexOf("/"));
		String nomeDoProjeto = xpews.substring(3, xpews.indexOf("/", 3));
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
		for (int i = 0; i < projects.length; i++) {
			if(projects[i].getName().compareToIgnoreCase(nomeDoProjeto) == 0){
				pastaProjetoXpews = projects[i].getLocation().toOSString();
				xpews = pastaProjetoXpews + arquivo; 
			}
		}
		return xpews;
	}

}
